package com.weiyin.examination.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.weiyin.examination.R;
import com.weiyin.examination.application.Constant;
import com.weiyin.examination.course.CourseA1;
import com.weiyin.examination.course.CourseA10;
import com.weiyin.examination.course.CourseA2;
import com.weiyin.examination.course.CourseA3;
import com.weiyin.examination.course.CourseA4;
import com.weiyin.examination.course.CourseA5;
import com.weiyin.examination.course.CourseA6;
import com.weiyin.examination.course.CourseA7;
import com.weiyin.examination.course.CourseA8;
import com.weiyin.examination.course.CourseA9;
import com.weiyin.examination.http.DataService;
import com.weiyin.examination.http.HttpRequestListener;
import com.weiyin.examination.http.RequestUrl;
import com.weiyin.examination.http.RetrofitClient;
import com.weiyin.examination.util.LayoutHelper;
import com.weiyin.examination.util.LogUtil;
import com.weiyin.examination.util.SPUtil;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.ResponseBody;

/**
 * 交卷完成界面
 *
 * @author njf
 */
public class SubmitActivity extends BaseActivity implements View.OnClickListener, HttpRequestListener<ResponseBody> {

    private Button submitBack;

    private Handler mHandler = new Handler();
    private String number, name, level, fileAudio1, fileAudio2;
    private TextView submitName;
    private DataService dataService;
    List<String> filePathList;
    int position;

    private List<String> imgList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_submit);
        filePathList = new ArrayList<>();
        imgList = new ArrayList<>();
        dataService = new DataService(RetrofitClient.getInstance(this).getService());
        LayoutHelper layoutHelper = new LayoutHelper(this);
        layoutHelper.scaleView(findViewById(R.id.submitActivityLayout));
        number = SPUtil.getSharedPreferencesData(this, "number", "");
        name = SPUtil.getSharedPreferencesData(this, "name", "");
        level = SPUtil.getSharedPreferencesData(this, "level", "");
        initView();
        fileAudio1 = SPUtil.getSharedPreferencesData(this, "courseAudio1", "");
        fileAudio2 = SPUtil.getSharedPreferencesData(this, "courseAudio2", "");

        if (!StringUtils.isEmpty(fileAudio1)) {
            filePathList.add(fileAudio1);
            position = 0;
        }
        if (!StringUtils.isEmpty(fileAudio2)) {
            filePathList.add(fileAudio2);
        }
        String count = SPUtil.getSharedPreferencesData(this, "titleNumber");
        if (Constant.isNumeric(count))
            for (int i = 0; i < Integer.valueOf(count); i++) {
                String filePath = SPUtil.getSharedPreferencesData(this, SPUtil.getSharedPreferencesData(this, "number") + "_" + (i + 1));
                LogUtil.i("submit", filePath);
                if (!StringUtils.isEmpty(filePath)){
                    filePathList.add(filePath);
                    imgList.add(filePath);
                }
            }
        if (position < filePathList.size())
            dataService.fileUpload(new File(filePathList.get(position)), this);
        updateScore();
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                skip();
            }
        }, 6000);
    }

    private void updateScore() {
        Map<String, String> map = new HashMap<>();
        String score1 = SPUtil.getSharedPreferencesData(this, CourseA1.class.getSimpleName());
        String score2 = SPUtil.getSharedPreferencesData(this, CourseA2.class.getSimpleName());
        String score3 = SPUtil.getSharedPreferencesData(this, CourseA3.class.getSimpleName());
        String score4 = SPUtil.getSharedPreferencesData(this, CourseA4.class.getSimpleName());
        String score5 = SPUtil.getSharedPreferencesData(this, CourseA5.class.getSimpleName());
        String score6 = SPUtil.getSharedPreferencesData(this, CourseA6.class.getSimpleName());
        String score7 = SPUtil.getSharedPreferencesData(this, CourseA7.class.getSimpleName());
        String score8 = SPUtil.getSharedPreferencesData(this, CourseA8.class.getSimpleName());
        String score9 = SPUtil.getSharedPreferencesData(this, CourseA9.class.getSimpleName());
        String score10 = SPUtil.getSharedPreferencesData(this, CourseA10.class.getSimpleName());

        String totalScore = String.valueOf(Double.valueOf(score1)
                + Double.valueOf(score2)
                + Double.valueOf(score3)
                + Double.valueOf(score4)
                + Double.valueOf(score5)
                + Double.valueOf(score6)
                + Double.valueOf(score7)
                + Double.valueOf(score8)
                + Double.valueOf(score9)
                + Double.valueOf(score10));
        String eachScore = "1-" + score1
                + ",2-" + score2
                + ",3-" + score3
                + ",4-" + score4
                + ",5-" + score5
                + ",6-" + score6
                + ",7-" + score7
                + ",8-" + score8
                + ",9-" + score9
                + ",10-" + score10;
        LogUtil.i("submit", "totalScore=" + totalScore + " eachScore=" + eachScore);
        String paperId = SPUtil.getSharedPreferencesData(this, "paperType", "");
        map.put("number", number);
        map.put("level", level);
        map.put("paperId", paperId);
        map.put("totalScore", totalScore);
        map.put("eachScore", eachScore);
        String file1 = "";
        String file2 = "";
        if (!StringUtils.isEmpty(fileAudio1)) {
            file1 = new File(fileAudio1).getName();
        }
        if (!StringUtils.isEmpty(fileAudio2)) {
            file2 = new File(fileAudio2).getName();
        }
        map.put("singingFileName", file1);
        map.put("soloFileName", file2);


        dataService.updateScore(map, this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacksAndMessages(null);
    }

    public void initView() {
        submitBack = (Button) findViewById(R.id.submitBack);
        submitName = (TextView) findViewById(R.id.submitName);

        submitName.setText(name + "   " + number);
        submitBack.setOnClickListener(this);
    }

    private void skip() {
        startActivity(new Intent(SubmitActivity.this, StartActivity.class));
        finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.submitBack:
                skip();
                break;
            default:
                break;
        }
    }

    @Override
    public void onCompleted(String url) {

    }

    @Override
    public void onError(Throwable e, String url) {
        LogUtil.i("submit", e.getMessage());
    }

    @Override
    public void onNext(ResponseBody responseBody, String url) {
        try {
            switch (url) {
                case RequestUrl.uploadScore:
                    String json1 = responseBody.string();
                    LogUtil.i("Submit", json1);
                    break;
                case RequestUrl.fileUpload:
                    String json = responseBody.string();
                    LogUtil.i("Submit", json);
                    try {
                        JSONObject jsonObject = new JSONObject(json);
                        if (jsonObject.getString("respCode").equals("0")) {
                            position++;
                            if (position < filePathList.size())
                                dataService.fileUpload(new File(filePathList.get(position)), this);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
